<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: install.php 155 2011-06-20 16:01:58Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

if (file_exists("../config.php")) {
        die("Configuration file config.php exists; aborting!");
}

$phpEx	= 'php';
require_once("../libs/template.$phpEx");

$basedir	= realpath(dirname(__FILE__)) . '/../';
$template	= new Template("$basedir/templates/install");
$template->set_filenames(array('header' => 'header.tpl', 'footer' => 'footer.tpl'));
$template->pparse('header');

if ($_REQUEST['op'] == '') {
	$template->set_filenames(array('body' => 'step0.tpl'));	
} else if ($_REQUEST['op'] == 'step1') {
	$fal_name	= $_REQUEST['fal'];
	if (file_exists("../libs/".$fal_name."_fal.$phpEx") && file_exists("../templates/install/step1_".$fal_name.".tpl")) {
		$tpl_name	= "step1_".$fal_name.".tpl";
		$template->set_filenames(array('body' => $tpl_name));
		if ($fal_name == 'wp2') {
			//
			// Try to auto-detect a WP install
			//
			$wp_cfg	= "unknown";
			if (file_exists("$basedir/../wp-config.php")) {
				$wp_cfg	= "$basedir/../wp-config.php";
			} else if (file_exists("$basedir/../wordpress/wp-config.php")) {
				$wp_cfg	= "$basedir/../wordpress/wp-config.php";
			} else if (file_exists("$basedir/../blog/wp-config.php")) {
				$wp_cfg	= "$basedir/../blog/wp-config.php";
			}

			if ($wp_cfg == "unknown") {
				$template->assign_block_vars('no_autodetect', array());
			} else {
				$template->assign_block_vars('yes_autodetect', array());
				require_once($wp_cfg);
				$template->assign_vars(array(
					"DB_NAME"	=> DB_NAME,
					"DB_HOST"	=> DB_HOST,
					"DB_USER"	=> DB_USER,
					"DB_PASS"	=> DB_PASSWORD,
					"WP2_PATH"	=> realpath(dirname($wp_cfg)),
					"WP2_PREFIX"	=> $table_prefix
				));
			}
		}
	} else {
		$template->set_filenames(array('body' => 'generic.tpl'));
		$template->assign_vars(array('MESSAGE' => "Error: unknown FAL: {$_REQUEST['fal']}, aborting."));
	}
} else if ($_REQUEST['op'] == 'step2') {
	$fal_name	= $_REQUEST['fal'];
	if (file_exists("../libs/".$fal_name."_fal.$phpEx") && file_exists("../templates/install/step2_".$fal_name.".tpl")) {
		$tpl_name	= "step2_".$fal_name.".tpl";
		$template->set_filenames(array('body' => $tpl_name));
		if ($fal_name == 'wp2') {
			$error	= '';
			$dbms	= 'mysql';
			require_once("../libs/db/mysql.$phpEx");
			require_once("../libs/constants.$phpEx");

			$wpdb	= new sql_db($_REQUEST['db_host'], $_REQUEST['db_user'], $_REQUEST['db_pass'], $_REQUEST['db_name'], false);
			if (!$wpdb->db_connect_id) {
				$error	= "Could not connect to the database {$_REQUEST['db_name']} @ {$_REQUEST['db_host']}";
			}

			if ($error == '') {
				$sql	= "SELECT option_name,option_value FROM {$_REQUEST['wp2_prefix']}options WHERE option_name IN ('blogname', 'siteurl')";
				$res	= $wpdb->sql_query($sql);
				if (($rows = $wpdb->sql_fetchrowset($res)) == NULL) {
					$errs	= $wpdb->error();
					$error	= "Error #{$errs['code']} retrieving blog title: {$errs['message']}";
				}
				//print_r($rows);
				foreach ($rows as $row) {
					if ($row['option_name'] == 'blogname') {
						$wp2_title	= $row['option_value'];
					} else if ($row['option_name'] == 'siteurl') {
						$wp2_url	= $row['option_value'];
					}
				}
			}

			if ($error != '') {
				print("XXX");
				$tpl_name	= "step1_wp2.tpl";
				$template->set_filenames(array('body' => $tpl_name));
				$template->assign_vars(array(
					"DB_NAME"	=> $_REQUEST['db_name'],
					"DB_HOST"	=> $_REQUEST['db_host'],
					"DB_USER"	=> $_REQUEST['db_user'],
					"DB_PASS"	=> $_REQUEST['db_pass'],
					"WP2_PATH"	=> $_REQUEST['wp2_path'],
					"WP2_PREFIX"	=> $_REQUEST['wp2_prefix']
				));
				$template->assign_block_vars('error', array(
					'MESSAGE'	=> $error
				));
			} else {
				$template->assign_vars(array(
					"DB_NAME"	=> $_REQUEST['db_name'],
					"DB_HOST"	=> $_REQUEST['db_host'],
					"DB_USER"	=> $_REQUEST['db_user'],
					"DB_PASS"	=> $_REQUEST['db_pass'],
					"WP2_PATH"	=> $_REQUEST['wp2_path'],
					"WP2_PREFIX"	=> $_REQUEST['wp2_prefix'],
					"WP2_URL"	=> $wp2_url,
					"WP2_TITLE"	=> $wp2_title
				));
			}
		}
	} else {
		$template->set_filenames(array('body' => 'generic.tpl'));
		$template->assign_vars(array('MESSAGE' => "Error: unknown or incomplete FAL: {$_REQUEST['fal']}, aborting."));
	}
} else {
	$req_vars	= print_r($_REQUEST, true);
	$template->set_filenames(array('body' => 'generic.tpl'));
	$template->assign_vars(array('MESSAGE' => "Error: unknown operation: {$_REQUEST['op']}, aborting.<br>\n<pre>$req_vars</pre>"));
}

$template->pparse('body');
$template->pparse('footer');
?>
